﻿#include <QBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <spdlog/spdlog.h>

#include "node_content_widget.h"
#include "node_node.h"

static bool NODECONTENTDEBUG = false;

QDMNodeContentWidget::QDMNodeContentWidget(std::weak_ptr<Node>node, QWidget* parent)
	:QWidget(parent)
{
	m_node = node;
	initUI();
	if (NODECONTENTDEBUG)
	{
		spdlog::info("NodeContentWidget create sucess");
	}

}

QDMNodeContentWidget::~QDMNodeContentWidget()
{
	if (NODECONTENTDEBUG)
	{
		spdlog::info("NodeContentWidget delete sucess");
	}
}

void QDMNodeContentWidget::initUI()
{
	auto Layout = new QVBoxLayout(this);
	Layout->setContentsMargins(0,0,0,0);//设置边距
	auto wdg_label = new QLabel("any Title",this);
	Layout->addWidget(wdg_label);
	Layout->addWidget(new QDMTextEdit("foo",this));
	setLayout(Layout);
}

void QDMNodeContentWidget::setEditingFlag(bool flag)
{
	m_editingFlag = flag;
}

const rapidjson::Document QDMNodeContentWidget::serialize()
{
	rapidjson::Document d; // 创建JSON对象  
	d.SetObject();
	d.AddMember("undefind", "temp_Nothing", d.GetAllocator());
	return d;
}

void QDMNodeContentWidget::deserialize(const rapidjson::Value& value)
{
}


QDMTextEdit::QDMTextEdit(const QString& text, QWidget* parent)
	:QTextEdit(text, parent)
{

}

void QDMTextEdit::keyPressEvent(QKeyEvent* e)
{
	QTextEdit::keyPressEvent(e);
}

void QDMTextEdit::focusInEvent(QFocusEvent* e)
{
	if (NODECONTENTDEBUG) {spdlog::info("focus in");}
	auto x = dynamic_cast<QDMNodeContentWidget*>(parentWidget());
	if (x!=nullptr)
	{
		x->setEditingFlag(true);
	}
	QTextEdit::focusInEvent(e);
}

void QDMTextEdit::focusOutEvent(QFocusEvent* e)
{
	if (NODECONTENTDEBUG) {spdlog::info("focus out");}
	auto x = dynamic_cast<QDMNodeContentWidget*>(parentWidget());
	if (x != nullptr)
	{
		x->setEditingFlag(false);
	}
	QTextEdit::focusOutEvent(e);
}


